﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lib472
{
    /// <summary>
    /// 数据类型
    /// </summary>
    public enum EdataType { 
        /// <summary>
        /// 字符串
        /// </summary>
        str,
        /// <summary>
        /// 整型
        /// </summary>
        eint
    };

    /// <summary>
    ///  系統類別
    /// </summary>
    public enum ImportsExportsType
    {
        /// <summary>
        /// 海運報關
        /// </summary>
        [Description("海運")]
        SeaTransit = 0,
        /// <summary>
        /// 空運報關
        /// </summary>
        [Description("空運")]
        AirTransit = 1,
        /// <summary>
        /// 空运快递
        /// </summary>
        [Description("快遞")]
        Express = 2,
        /// <summary>
        /// 空運倉單
        /// </summary>
        [Description("空運倉單")]
        CabinBillA = 3,
        /// <summary>
        /// 海運倉單
        /// </summary>
        [Description("海運倉單")]
        CabinBillS = 4,
        /// <summary>
        /// 港区彙報
        /// </summary>
        [Description("港区彙報")]
        MonthTotal = 5,
        /// <summary>
        /// 海運倉儲
        /// </summary>
        [Description("海運倉儲")]
        StorageCabinBill = 6,
        CabinBill=7,
        GoodsTransit=8,
        AirCabinBill=9,
        SeaCabinBill=10



    };

    /// <summary>
    /// 進出口別
    /// </summary>
    public enum importExportKind
    {
        /// <summary>
        /// 进口
        /// </summary>
        /// 
        [Description("進口")]
        import = 0,
        /// <summary>
        /// 出口
        /// </summary>
        [Description("出口")]
        Export = 1,
        /// <summary>
        /// 转运
        /// </summary>
        [Description("转运")]
        transport = 2


    }

    /// <summary>
    /// 傳送接收類型
    /// </summary>
    public enum SendReceive
    {
        /// <summary>
        /// 傳送
        /// </summary>
        [Description("傳送")]
        Send = 0,
        /// <summary>
        /// 接收
        /// </summary>
        [Description("接收")]
        Receive = 1

    }

    public enum EdiInformationType
    {
        /// <summary>
        /// NX5106 單證合一核覆訊息
        /// </summary>
        [Description("單證合一核覆訊息")]
        NX5106,
        /// <summary>
        /// N5107 應補辦事項通知
        /// </summary>
        [Description("應補辦事項通知")]
        N5107,
        /// <summary>
        /// N5109 查驗貨物通知
        /// </summary>
        [Description("查驗貨物通知")]
        N5109,
        /// <summary>
        /// N5110 進口貨物稅費繳納證兼匯款申請書
        /// </summary>
        [Description("進口貨物稅費繳納證兼匯款申請書")]
        N5110,
        /// <summary>
        /// N5111 國庫專戶存款收款書兼匯款申請書
        /// </summary>
        [Description("國庫專戶存款收款書兼匯款申請書")]
        N5111,
        /// <summary>
        /// N5116 進口貨物放行通知
        /// </summary>
        [Description("進口貨物放行通知")]
        N5116,
        /// <summary>
        /// N5204 出口貨物放行通知
        /// </summary>
        [Description("出口貨物放行通知")]
        N5204,
        /// <summary>
        /// N5302 轉運准單
        /// </summary>
        [Description("轉運准單")]
        N5302,
        /// <summary>
        /// N5168 無法吊櫃通知
        /// </summary>
        [Description("無法吊櫃通知")]
        N5168,
        /// <summary>
        /// N5108 倉儲或運輸業申報訊息回覆通知
        /// </summary>
        [Description("倉儲或運輸業申報訊息回覆通知")]
        N5108,
        /// <summary>
        /// N5158 卸貨准單
        /// </summary>
        [Description("卸貨准單")]
        N5158,
        /// <summary>
        /// N5166 進口貨櫃清單
        /// </summary>
        [Description("進口貨櫃清單")]
        N5166,
        /// <summary>
        /// N5170 作業指示通知
        /// </summary>
        [Description("作業指示通知")]
        N5170,
        /// <summary>
        /// N5402 自由港區事業按月彙報貨物核准通知
        /// </summary>
        [Description("自由港區事業按月彙報貨物核准通知")]
        N5402,
        /// <summary>
        /// N5403 自由港區事業按月彙報貨物不受理或取消核准通知
        /// </summary>
        [Description("自由港區事業按月彙報貨物不受理或取消核准通知")]
        N5403,
        /// <summary>
        /// NX903 錯誤或異常通知訊息
        /// </summary>
        [Description("錯誤或異常通知訊息")]
        NX903
    }

    /// <summary>
    /// 數據庫類型
    /// </summary>
    public enum SourceType
    {
        CBSDATA,
        CODE,
        EDI,
        XMLSET,
        DocData,
        CbsSetContent,
        TableInf,
        CanNotIdentify,
        XmlSetContent,
        Catk
    }


    public class EdiTypes
    {





        public static string GetDescription(Enum @enum)
        {
            var field = @enum.GetType().GetField(@enum.ToString());

            var attribute=(DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
            return attribute?.Description?? @enum.ToString();
        }


        public static string importsExportsCode(importExportKind importExportKind, ImportsExportsType importsExportsType)
        {
            return $"{importExportKindCode(importExportKind)}{ImportsExportsTypeCode(importsExportsType)}"; 
        }

        public static string ImportsExportsTypeCode(ImportsExportsType importsExportsType)
        {
            
            switch(importsExportsType)
            {
                case ImportsExportsType.SeaTransit:
                    {
                        return "S";
                    }
                case ImportsExportsType.AirTransit:
                    {
                        return "A";
                    }
                default:
                    {
                        return string.Empty;
                    }
            }
        }

        public static string importExportKindCode(importExportKind importExportKind)
        {
            switch (importExportKind)
            {

                case importExportKind.import:
                    {
                        return "5105";
                    }
                case importExportKind.Export:
                    {
                        return "5203";
                    }
                case importExportKind.transport:
                    {
                        return "5301";
                    }
                default:
                    {
                        return string.Empty;
                    }
            }

           
        }

        public static void GetimportExportKind(string Msgcode,out importExportKind importExportKind, out ImportsExportsType importsExportsType)
        {
            string msg = string.Empty;
            string code = string.Empty;
            foreach (char c in Msgcode)
            {
                if (char.IsDigit(c))
                {
                    msg += c;
                }
                else
                {
                    code += c;
                }
            }
            switch (msg)
            {

                case "5105":
                    {
                        importExportKind = importExportKind.import;
                        break;
                    }
                case "5203":
                    {
                        importExportKind = importExportKind.Export;
                        break;
                    }
                case "5301":
                    {
                        importExportKind = importExportKind.transport;
                        break;
                    }
                default:
                    {
                        throw new NotImplementedException(nameof(importExportKind));
                    }
                   
            }

            switch (code)
            {
                case "S":
                    {
                        importsExportsType = ImportsExportsType.SeaTransit;
                        break;
                    }
                case "A":
                    {
                        importsExportsType = ImportsExportsType.AirTransit;
                        break;
                    }
                default:
                    {
                        throw new NotImplementedException(nameof(importsExportsType));
                    }
            }
        }

        /// <summary>
        /// 獲取進出口別在Mysql中的代碼
        /// </summary>
        /// <param name="exportKind"></param>
        /// <returns></returns>
        public static string GetimportExportKindForMysql(importExportKind exportKind)
        {
            switch (exportKind)
            {

                case importExportKind.import:
                    {
                        return "I";
                    }
                case importExportKind.Export:
                    {
                        return "E";
                    }
                   
            }
            return string.Empty;
        }

        /// <summary>
        /// 進出口別數據數字轉換成代碼
        /// </summary>when 1 then '5105' when 2 then '5203' else '5301' 
        /// <param name="nub"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static string importExportNubToCode(int nub)
        {
            switch (nub)
            {
                case 1:
                    {
                        return "5105";
                    }
                case 2:
                    {
                        return "5203";
                    }
                case 3:
                    {
                        return "5301";
                    }
                default:
                    {
                        throw new NotImplementedException(nameof(nub));
                    }
            }
        }

        /// <summary>
        /// 海空運別轉換成代碼
        /// </summary>
        /// <param name="nub"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static string AirSeaNubToCode(int nub)
        {
            switch (nub)
            {
                case 1:
                    {
                        return "S";
                    }
                case 4:
                    {
                        return "A";
                    }
                default:
                    {
                        throw new NotImplementedException(nameof(nub));
                    }
            }
        }

        /// <summary>
        /// 海空運別轉換成中文
        /// </summary>
        /// <param name="nub"></param>
        /// <returns></returns>
        public static string AirSeaNubToName(string nub)
        {

            int code;

            if (!int.TryParse(nub, out code)) return string.Empty;

            switch (code)
            {
                case 1:
                    {
                        return "海運";
                    }
                case 4:
                    {
                        return "空運";
                    }
                default:
                    {
                        throw new NotImplementedException(nameof(nub));
                    }
            }

        }

        /// <summary>
        /// 回應狀況代碼轉換
        /// </summary>
        /// <param name="restoreSign"></param>
        /// <returns></returns>
        public static string RestoreAccept(string restoreSign)
        {
            if (restoreSign == string.Empty) return string.Empty;

            if (restoreSign == "N") return "不受理";
            if (restoreSign == "Y") return "受理";
            return string.Empty;
        }


        /// <summary>
        /// 獲取訊息功能
        /// </summary>
        /// <param name="infoSign"></param>
        /// <returns></returns>
        public static string GetInfoFunction(string infoSign)
        {
            if (infoSign == string.Empty) return string.Empty;

            if (infoSign == "5")
            {
                return "替換";
            }
            else
            {
                return "新增";
            }
        }

    }

}
